//导入axions
import axios from 'axios'

//获取token
const AUTH_TOKEN = (() => {
  return localStorage.getItem("token")
})()

// 全局配置
const instance  = axios.create( {
  // baseURL: "/api"
  baseURL: "https://api.it120.cc/b16a7808b6a4205a089400a9d778d4f6"
})
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
// 请求拦截器
instance.interceptors.request.use( (config) => {
  let url = config.url
  if( url.indexOf("login") > -1 ) {
    localStorage.setItem( "token", "")
    config.headers.common['Authorization'] = ""
  } 
  if( url.indexOf("user") > -1 && url.indexOf("login") < 0 ) {
    config.headers.common['Authorization'] = localStorage.getItem( "token" )
  }
  return config
}, (err) => {
  return Promise.reject(err)
})
// 响应拦截器
instance.interceptors.response.use( (res) => {
  if(res.headers.token){
    localStorage.setItem('token',res.headers.token);
  }
  return res
}, (err) => {
  return err
})

export default instance